{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 动态物体捕捉\n",
    "\n",
    "- 1.准备一个背景分割器\n",
    "- 2.准备画面\n",
    "- 3.把画面送入追踪器\n",
    "- 4.对画质进行一系列图形学处理\n",
    "- 5.提取轮廓并标记"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "camera = cv2.VideoCapture(0)\n",
    "knn = cv2.createBackgroundSubtractorKNN()\n",
    "while True:\n",
    "    ok,image = camera.read()\n",
    "    if ok:\n",
    "        new = knn.apply(image)\n",
    "        blur = cv2.GaussianBlur(new,(15,15),0)\n",
    "        _,binary = cv2.threshold(blur,100,255,8)\n",
    "        #轮廓提取\n",
    "        cnts,_ = cv2.findContours(binary,3,2)\n",
    "        #遍历所有的轮廓，并画框标记\n",
    "        for i in cnts:\n",
    "            x,y,w,h = cv2.boundingRect(i) #送入轮廓，返回 x,y,w,h\n",
    "            if 60<w<400 and 60<h<400: #过滤\n",
    "                cv2.rectangle(image,(x,y,w,h),(0,255,0),3)\n",
    "        #imshows\n",
    "        cv2.imshow('image',image)\n",
    "        cv2.imshow('binary',binary)\n",
    "        if cv2.waitKey(10) > 0:\n",
    "            break\n",
    "    else:\n",
    "        break\n",
    "cv2.destroyAllWindows()\n",
    "camera.release()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
